using Common;
using Interface;
using Microsoft.AspNetCore.Mvc;
using Model.Dto.Teacher;
using Model.Entity;

namespace Api.Controllers;

/// <summary>
/// 认证控制器
/// </summary>
/// <remarks>
/// 提供用户认证相关的API接口，包括教师登录等功能
/// </remarks>
[ApiController]
[Route("api/auth")]
public class AuthController : ControllerBase
{
    private readonly ITeacherService _teacherService;
    private readonly ILogger<AuthController> _logger;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="teacherService">教师服务</param>
    /// <param name="logger">日志记录器</param>
    public AuthController(ITeacherService teacherService, ILogger<AuthController> logger)
    {
        _teacherService = teacherService;
        _logger = logger;
    }

    /// <summary>
    /// 教师登录
    /// </summary>
    /// <param name="dto">教师登录数据传输对象</param>
    /// <returns>登录结果</returns>
    /// <response code="200">登录成功或失败</response>
    /// <remarks>
    /// RESTful规范：POST /api/auth
    /// </remarks>
    [HttpPost]
    public async Task<R<bool>> TeacherLogin([FromBody] TeacherLogin dto)
    {
        try
        {
            var result = await _teacherService.LoginAsync(dto);
            return result ? R<bool>.Success("登录成功") : R<bool>.Error("登录失败");
        }
        catch (BusinessException e)
        {
            _logger.LogWarning("教师登录失败 - 手机号: {Mobile}, 原因: {Message}", dto.Mobile, e.Message);
            return R<bool>.Error(e.Message);
        }
        catch (Exception e)
        {
            _logger.LogError(e, "教师登录异常 - 手机号: {Mobile}", dto.Mobile);
            return R<bool>.Error("登录失败，请稍后重试");
        }
    }
}